package com.xyu.gulimall.product.service.impl;

import com.xyu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.xyu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.xyu.gulimall.product.entity.AttrEntity;
import com.xyu.gulimall.product.service.AttrService;
import com.xyu.gulimall.product.vo.AttrGroupRelationVo;
import com.xyu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyu.common.utils.PageUtils;
import com.xyu.common.utils.Query;

import com.xyu.gulimall.product.dao.AttrGroupDao;
import com.xyu.gulimall.product.entity.AttrGroupEntity;
import com.xyu.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

//属性分组表
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrAttrgroupRelationDao relationDao;
    @Resource
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //查询当前分组的所有信息
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );
        return new PageUtils(page);
    }

    //查询当前分组的所有信息，也可以进行模糊查询
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //获取查询参数[由前端返回]
        String key = (String) params.get("key");
        //创建条件构造器
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        //查询参数不为空则进行查询
        if (!StringUtils.isEmpty(key)) {
            //构造查询条件
            wrapper.and((obj) -> obj.eq("attr_group_id", key).or().like("attr_group_name", key));
        }
        //如果商品id为0则直接进行查询
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        } else {
            //否则根据id精确查询
            wrapper.eq("catelog_id", catelogId); //wrapper = select * from
        }
//select * from pms_attr_group where catelogId=catelog_id and(attrgroupId=attr_group_id or attrgroupName=attr_group_name);
        //通过查询条件创建查询[第一个参数分页查询信息，wrapper查询条件]
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }
    /**
     *17、获取分类下所有分组&关联属性
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //查询所有分组属性
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //操作流
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(item -> {
            //创建VO实体类
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            //copy查询的基本信息
            BeanUtils.copyProperties(item, attrGroupWithAttrsVo);
            //根据group查询所有分组属性
            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());
            //放到VO实体类中
            attrGroupWithAttrsVo.setAttrs(relationAttr);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
